package com.angel.controller;

import com.angel.bean.Employee;
import com.angel.bean.Problems;
import com.angel.bean.dto.ProblemsDto;
import com.angel.service.OtherlogService;
import com.angel.service.ProblemsService;
import com.angel.shiro.SystemAdminAuthorizingRealm;
import com.angel.utils.AdminUserUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/pro")
public class ProblemsController {
    @Autowired
    private ProblemsService problemsService;
    @Autowired
    private OtherlogService otherlogService;
    /**
     * 获取问题列表及添加问题
     *
     * @return
     */
    @RequestMapping("/getPlist")
    public String getProblems() {
        return "problems";
    }


    @RequestMapping("/addpro")
    @ResponseBody
    public Map addProblems(Problems problems) {
        Map map = new HashMap();
        int result = 0;
        map.put("result", false);
        SystemAdminAuthorizingRealm.Principal principal = AdminUserUtils.getPrincipal();
        Employee employee = principal.getEmployee();
        if (problems != null && problems.getContent() != null && !"".equals(problems.getContent())) {
            if (employee != null) {
                problems.setCreateUser(employee.getEmId());
                problems.setCreatetime(new Timestamp(System.currentTimeMillis()));
                problems.setStatus(1);
                result = problemsService.insert(problems);
            }
            if (result != 0) {
                map.put("result", true);
                map.put("data", problems);
            }
        }
        return map;
    }


    @RequestMapping("/getlist")
    public String getProBlemList(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {
        //将前端传回的参数改为String类型，然后进行数据类型转换，如果出现问题，则进行异常处理，异常可以是空处理
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {
            pageNo = 1;
        }
        //在查询之前传入当前页，然后多少记录
        PageHelper.startPage(pageNo, 5);
        List<ProblemsDto> problemslist = problemsService.selectAllHasName(new Problems());
        PageInfo pageInfo = new PageInfo(problemslist);
        if (problemslist != null && !problemslist.isEmpty()) {
            model.addAttribute("pageInfo", pageInfo);
        }
        return "problemslist";
    }

    @RequestMapping("/getproone")
    @ResponseBody
    public Map getProblems(@RequestParam(value = "pid") String pid) {
        Map map = new HashMap();
        map.put("result", false);
        int id = 0;
        try {
            id = Integer.valueOf(pid);
        } catch (Exception e) {
            return map;
        }
        Problems problems = new Problems();
        problems.setPid(id);
        problems = problemsService.getOne(problems);
        if (problems != null) {
            map.put("result", true);
            map.put("data", problems);
        }
        return map;
    }

    @RequestMapping("/setStatus")
    @ResponseBody
    public Map updateProblem(Problems problems) {
        Map map = new HashMap();
        map.put("result", false);
        if (problems.getPid() != null) {
            if (problems.getStatus() != null) {
                if (problems.getStatus() != 0 && problems.getStatus() != 1) {
                    problems.setStatus(1);
                }
            }
            int res = problemsService.update(problems);
            if (res != 0) {
                map.put("result", true);
            }
        }
        return map;
    }

}
